<?php
declare (strict_types = 1);

namespace app\controller;

use app\service\CategoryService;
use app\service\HouseService;
use think\facade\Validate;
use think\Request;
use app\common\BaseController;
use app\service\GoodsService;

class GoodsController extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
      $page = $request->param('page', 1);
      $pageSize = $request->param('pageSize', 10);
      $category_id = $request->param('category_id', '');
      $process = $request->param('process', '');
      $sellable = $request->param('sellable', false);
      $name = $request->param('name', '');
      $isHouse = $request->param('isHouse', 0);

      // 是否查询库存
      $conditions = [];
      if(strlen($category_id) > 0){
        $conditions[] = ['category_id',$category_id,'='];
      }
      if(strlen($process) > 0){
        $conditions[] = ['process',$process,'='];
      }
      if($name){
        $conditions[] = ['name','%'.$name.'%','LIKE'];
      }
      if ($sellable) {
        $conditions[] = ['sellable', 1, '='];
        $conditions[] = ['price', 0, '>'];
      }
      $list = GoodsService::getGoodsByCondition($conditions, ['id' => 'desc']);
      // 获取库存
      $houseList = HouseService::getInstance()->getHouseJoinGoods([['count', 0, '>']]);
      $houseList = array_column($houseList, null, 'goods_id');
      // 获取分类
      $category = CategoryService::getInstance()->getCategoryByCondition([['base', 0, '=']]);
      $category = array_column($category, 'category_name', 'id');
      foreach ($list as $key => &$value) {
        if ($isHouse && (!isset($houseList[$value['id']]) || !$houseList[$value['id']]['count'])) {
          unset($list[$key]);
        }
        $value['category'] = $category[$value['category_id']];
        $value['house_count'] = isset($houseList[$value['id']]) ? $houseList[$value['id']]['count'] : 0;
        $value['cost'] = isset($houseList[$value['id']]) ? $houseList[$value['id']]['price'] : 0;
      }
      // 分页
      $count = count($list); //总条数
      $start=intval(($page - 1) * $pageSize); //偏移量，当前页-1乘以每页显示条数
      $pager = [
        'totalCount' => $count,
        'pageSize' => intval($pageSize),
        'currPage' => intval($page),
        'totalPage' => intval($count/$pageSize)+1
      ];
      $data = array_slice($list, $start, intval($pageSize));
      return $this->send200ResponseWithPager($data,$pager);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
      $data = [];
      $data['category_id'] = $request->param('category_id','', '');
      $data['process'] = $request->param('process','', '');
      $data['name'] = $request->param('name','', '');
      $data['price_in'] = $request->param('price_in','', '');
      $data['unit_in'] = $request->param('unit_in','', '');
      $data['sellable'] = $request->param('sellable','', '');
      $data['specs'] = $request->param('specs', '');
      $data['conversion'] = $request->param('conversion','', '');
      $data['sale_name'] = $request->param('sale_name','', '');
      $data['price'] = $request->param('price','', '');
      $data['unit'] = $request->param('unit','', '');

      $validate = Validate::rule([
        'category_id' => 'require',
        'process' => 'require',
        'name' => 'require',
        'price_in' => 'require',
        'unit_in' => 'require',
        'sellable' => 'require',
        'conversion' => 'require',
        'sale_name' => 'require',
        'price' => 'require',
        'unit' => 'require',
      ])->message([
        'category_id.require' => '分类不能为空',
        'process.require' => '品类不能为空',
        'name.require' => '商品名不能为空',
        'price_in.require' => '默认进货价不能为空',
        'unit_in.require' => '进货单位不能为空',
        'sellable.require' => '是否可销售不能为空',
        'conversion.require' => '转换比例不能为空',
        'sale_name.require' => '销售名不能为空',
        'price.require' => '售价不能为空',
        'unit.require' => '销售单位不能为空',
      ]);
      if (!$validate->check($data)) {
        return $this->send400Response($validate->getError());
      }
      if (!$data['conversion']) {
        return $this->send400Response('转换比例不得为0');
      }
      $result = GoodsService::createGoods($data);
      if ($result['status']) {
        return $this->send200Response('ok');
      } else {
        return $this->send400Response($result['msg']);
      }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
      $goods = GoodsService::getGoodsById($id);
      return $this->send200Response($goods);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
      $data = [];
      $data['price_in'] = $request->param('price_in','');
      $data['unit_in'] = $request->param('unit_in','');
      $data['sellable'] = $request->param('sellable','');
      $data['sale_name'] = $request->param('sale_name','');
      $data['conversion'] = $request->param('conversion','', '');
      $data['price'] = $request->param('price','');
      $data['unit'] = $request->param('unit','');
      $data['category_id'] = $request->param('category_id','');
      $validate = Validate::rule([
        'price_in' => 'require',
        'unit_in' => 'require',
        'sellable' => 'require',
        'sale_name' => 'require',
        'price' => 'require',
        'conversion' => 'require',
        'unit' => 'require',
        'category_id' => 'require'
      ])->message([
        'price_in.require' => '默认进货价不能为空',
        'unit_in.require' => '进货单位不能为空',
        'sellable.require' => '是否可销售不能为空',
        'sale_name.require' => '销售名不能为空',
        'price.require' => '售价不能为空',
        'conversion.require' => '转换比例不能为空',
        'unit.require' => '销售单位不能为空',
        'category_id.require' => '分类不能为空'
      ]);
      if (!$validate->check($data)) {
        return $this->send400Response($validate->getError());
      }
      $result = GoodsService::updateGoods($id,$data);
      if ($result['status']) {
        return $this->send200Response('ok');
      } else {
        return $this->send400Response($result['msg']);
      }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 门店搜索可售商品
     */
    public function getStoreGoods(Request $request) {
      $category_id = $request->param('category_id', '');
      $base = $request->param('base', '');
      if (!$base) {
        return $this->send400Response('base必填');
      }
      $conditions = [
        ['g.sellable', 1, '='],
        ['g.price', 0, '>']
      ];
      if(strlen($category_id) > 0){
        $conditions[] = ['g.category_id', $category_id, '='];
      }
      $order = ['sh.last_update_time'=>'desc', 'g.id'=>'desc'];
      $list = GoodsService::getInstance()->getStoreGoods($base, $conditions, $order);
      return $this->send200Response($list);
    }
}
